package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Sports;
import com.itheima.service.SportsService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author houzongtao
 * @date 2020/9/1 14:36
 */
@RestController
@RequestMapping("/sports")
public class SportsController {
    @Reference
    private SportsService sportsService;
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            //读取Excel文件数据
            List<String[]> list = POIUtils.readExcel(excelFile);
            if (list != null && list.size() > 0) {
                List<Sports> sportsList = new ArrayList<>();
                for (String[] strings : list) {
                    Sports sports = new Sports(
                            Integer.parseInt(strings[0]),
                            strings[1],
                            strings[2],
                            strings[3],
                            strings[4]);
                    sportsList.add(sports);
                }
                sportsService.batchAdd(sportsList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_SPORTS_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_SPORTS_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = sportsService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Sports sports){
        try {
            sportsService.add(sports);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_SPORTS_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SPORTS_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Sports sports = sportsService.findById(id);
            return new Result(true, MessageConstant.QUERY_SPORTS_SUCCESS,sports);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_SPORTS_FAIL);
        }
    }
    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Sports sports){
        try {
            sportsService.edit(sports);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_SPORTS_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_SPORTS_SUCCESS);
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            sportsService.delete(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_SPORTS_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_SPORTS_SUCCESS);
    }
}
